'''
Created on Mar 23, 2013

@author: Petko
'''
import unittest
from utils.sententialyzer_client import SententialyzerClient
from sententialyzer.settings import SENTENTIALYZER_ADMIN_USER_NAME,\
    SENTENTIALYZER_DEFAULT_GROUP_NAME
import json
from urllib2 import HTTPError


class setup_moderators(unittest.TestCase):
    sc = SententialyzerClient()

    def setUp(self):
        pass


    def tearDown(self):
        pass


    def testSetupModetators(self):
        #self.sc.authorize('testname', 'testpass', 'noscope')
        
        self.sc.authorize('moderator1', 'testpass', 'bopin_r')
        self.sc.authorize('moderator2', 'testpass', 'bopin_r')
        self.sc.authorize('group_moderator', 'testpass', 'bopin_r')
        
        self.sc.authorize(SENTENTIALYZER_ADMIN_USER_NAME, 'testpass', 'group_adm')
        
        group = self.sc.createGroup('group_moderators', 'empty', 'group_adm')
        print json.dumps(group)
        
        print self.sc.addUserToGroup('group_moderator', 'group_moderators')
        
        self.sc.authorize('group_moderator', 'testpass', 'group_adm')
        
        group = self.sc.createGroup('moderators', 'empty', 'bopin_c bopin_d bopin_r')
        print json.dumps(group)
        
        #try to create group with invalid name
        with self.assertRaises(HTTPError) as cm:
            group = self.sc.createGroup('moderators:', 'empty', 'bopin_c bopin_d bopin_r')
        self.assertEqual(cm.exception.code, 400)
        
        group = self.sc.createGroup('view_admins', 'none', 'view_adm')
        
        print self.sc.addUserToGroup('moderator2', 'moderators')
        print self.sc.addUserToGroup('moderator1', 'moderators')
        print self.sc.addUserToGroup('moderator1', 'view_admins')
        
        self.sc.authorize('moderator1', 'testpass', 'bopin_r bopin_c bopin_d')
        self.sc.authorize('moderator2', 'testpass', 'bopin_r bopin_c bopin_d')
        
        self.sc.authorize('group_moderator', 'testpass', 'group_adm')
        
        print self.sc.removeUserFromGroup('moderator2', 'moderators')
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.removeUserFromGroup('moderatorXXX', 'moderators')
        self.assertEqual(cm.exception.code, 404)

        with self.assertRaises(HTTPError) as cm:
            self.sc.removeUserFromGroup('moderator2', 'moderatorsYYY')
        self.assertEqual(cm.exception.code, 404)
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.authorize('moderator2', 'testpass', 'bopin_r bopin_c bopin_d')
        self.assertEqual(cm.exception.code, 403)
        
        moderatorsJson = self.sc.getGroupUsers('moderators')
        print json.dumps(moderatorsJson)
        
        self.assertEqual(moderatorsJson['totalResults'], 1)
        
        self.assertEqual(moderatorsJson['page'][0]['id'], 'moderator1')
        
        self.sc.authorize(SENTENTIALYZER_ADMIN_USER_NAME, 'testpass', 'udat_adm')
        
        moderator1Groups = self.sc.getUserGroups('moderator1')
        
        print json.dumps(moderator1Groups)
        
        self.assertEqual(moderator1Groups['totalResults'], 3)
        

        
    def _testOpinion(self):
        self.sc.authorize('moderator1', 'testpass', 'bopin_c')
        self.sc.createBaseOpinion("ola ola", "ola ola ola ola", SENTENTIALYZER_DEFAULT_GROUP_NAME + ":r moderators:rcd" )

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()